package com.gtw.modules.sys.controller;

import com.gtw.common.annotation.SysLog;
import com.gtw.common.utils.PageUtils;
import com.gtw.common.utils.Query;
import com.gtw.common.utils.R;
import com.gtw.modules.entity.TbDiscount;
import com.gtw.modules.sys.service.SysDiscountService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**优惠controller
 * Created by wuye on 2018/1/31.
 */
@RestController
@RequestMapping("/sys/discount")
public class SysDiscountController extends AbstractController{

    @Autowired
    private SysDiscountService discountService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:discount:list")
    public R list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);

        List<TbDiscount> discountList = discountService.queryList(query);
        int total = discountService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(discountList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:discount:info")
    public R info(@PathVariable("id") Integer id){
        TbDiscount discount = discountService.queryObject(id);

        return R.ok().put("discount", discount);
    }

    /**
     * 保存
     */
    @SysLog("添加")
    @RequestMapping("/save")
    @RequiresPermissions("sys:discount:save")
    public R save(@RequestBody TbDiscount discount){
        discount.setCreatetime(new Date());
        discount.setCreateuser(getUser().getUsername());
        discount.setUpdatetime(new Date());
        discount.setUpdateuser(getUser().getUsername());
        discountService.save(discount);
        return R.ok();
    }

    /**
     * 修改
     */
    @SysLog("修改")
    @RequestMapping("/update")
    @RequiresPermissions("sys:discount:update")
    public R update(@RequestBody TbDiscount discount){
        discount.setUpdatetime(new Date());
        discount.setUpdateuser(getUser().getUsername());
        discountService.update(discount);
        return R.ok();
    }

    /**
     * 删除
     */
    @SysLog("删除")
    @RequestMapping("/delete")
    @RequiresPermissions("sys:discount:delete")
    public R delete(@RequestBody Integer[] ids){
        discountService.deleteBatch(ids);
        return R.ok();
    }
}
